<template>
  <image
    :lazy-load="node.attr.lazyLoad"
    :class="[node.classStr,'wxParse-img']"
    @click.stop="wxParseImgTap"
    @load="wxParseImgLoad"
		:data-from="node.from" :data-idx="node.imgIndex" :data-src="node.attr.src" :mode="node.attr.mode?node.attr.mode:'widthFix'" :src="node.attr.src" :style="'width:'+node.width+'px;'"
    />
</template>

<script>var myVue = {};
export default {
  name: 'wxParseImg',
  data() {
    return {
      newStyleStr: '',
      preview: true,
    };
  },
  props: {
    node: {
      type: Object,
      default() {
        return {};
      },
    },
  },
  methods: {
    wxParseImgTap(e) {
      if (!this.preview) return;
      const { src } = e.currentTarget.dataset;
      if (!src) return;
      this.getApp.core.previewImage({
				urls: [src]
			});
    },
    // 图片视觉宽高计算函数区
    wxParseImgLoad(e) {
      const { src } = e.currentTarget.dataset;
      if (!src) return;
      const { width, height } = e.mp.detail;
      const recal = this.wxAutoImageCal(width, height);
      const { imageheight, imageWidth } = recal;
      const { padding, mode } = this.node.attr;
      const { styleStr } = this.node;
      const imageHeightStyle = mode === 'widthFix' ? '' : `height: ${imageheight}upx;`;
      this.newStyleStr = `${styleStr}; ${imageHeightStyle}; width: ${imageWidth}upx; padding: 0 ${+padding}upx;`;
    },
    // 计算视觉优先的图片宽高
    wxAutoImageCal(originalWidth, originalHeight) {
      // 获取图片的原始长宽
      const { padding } = this.node.attr;
      const windowWidth = padding ? this.getApp.core.getSystemInfoSync().windowWidth*2 - (2 * padding):this.getApp.core.getSystemInfoSync().windowWidth*2;
      const results = {};

      if (originalWidth < 60 || originalHeight < 60) {
        const { src } = this.node.attr;
        this.node.$host.removeImageUrl(src);
        this.preview = false;
      }

      // 判断按照那种方式进行缩放
      if (originalWidth > windowWidth) {
        // 在图片width大于手机屏幕width时候
        results.imageWidth = windowWidth;
        results.imageheight = windowWidth * (originalHeight / originalWidth);
      } else {
        // 否则展示原来的数据
        results.imageWidth = originalWidth;
        results.imageheight = originalHeight;
      }
			
      return results;
    },
  },
};
</script>
